package com.eliza.base.views.database.crud

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Button
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.eliza.base.entity.model.user.UserViewModel
import com.eliza.library.data.entity.UserEntity
import kotlinx.coroutines.launch


@Composable
fun DataBaseCrudScreen(
    it: NavController,
    userVm: UserViewModel = hiltViewModel(),
) {
    val listState = rememberLazyListState()
    val coroutineScope = rememberCoroutineScope()
    val dataList by userVm.getAllFlow().collectAsState(listOf())
    val userName = remember {
        mutableStateOf("")
    }
    val userAlia = remember {
        mutableStateOf("")
    }
    Column {
        VmBtn(userName, userAlia, userVm)
        Row {
            LazyColumn(state = listState) {
//                items(userVm.list.value) {
                items(dataList) {
                    TextButton(onClick = {
                        coroutineScope.launch {
                            userVm.deleteById(UserEntity(it.id, "", ""))
                        }
                    }) {
                        Text(text = "Del UserResource: ${it.toString()}")
                    }
                }
            }
        }
        Row {
            LazyColumn(state = listState) {
                val sdf = userVm.getAllNormal().value ?: listOf(UserEntity(0, "00", "00"))
                Log.d("DataBaseCrudScreen", sdf.toString())
                items(sdf) {
                    TextButton(onClick = {
                        coroutineScope.launch {
                            userVm.deleteById(UserEntity(it.id, "", ""))
                        }
                    }) {
                        Text(text = "del livedata: ${it.toString()}")
                    }
                }
            }
        }
    }
}

@Composable
private fun VmBtn(
    userName: MutableState<String>,
    userAlia: MutableState<String>,
    userVm: UserViewModel
) {
    Row {
        TextField(
            modifier = Modifier.weight(1f),
            colors = TextFieldDefaults.colors(
                focusedContainerColor = MaterialTheme.colorScheme.surface,
                unfocusedContainerColor = Color.Gray,
                disabledContainerColor = MaterialTheme.colorScheme.surface,
            ),
            value = userName.value,
            onValueChange = { userName.value = it })
        TextField(
            modifier = Modifier.weight(1f),
            value = userAlia.value,
            colors = TextFieldDefaults.colors(
                focusedContainerColor = MaterialTheme.colorScheme.surface,
                unfocusedContainerColor = Color.Yellow,
                disabledContainerColor = MaterialTheme.colorScheme.surface,
            ),
            onValueChange = { userAlia.value = it })
    }
    Row {
        Button(onClick = {
            userVm.insert(listOf(UserEntity(0, userName.value, userAlia.value)))
        }) {
            Text(text = "insert")
        }
        Button(onClick = {
        }) {
            Text(text = "GetAll UserEntity")
        }
    }
    Row {
        Button(onClick = {
            userVm.getByName(userName.value)
        }) {
            Text(text = "getByName")
        }
    }
}

@Composable
private fun navigationDrawerSample(drawerContent: @Composable () -> Unit) {
    val drawerState = rememberDrawerState(DrawerValue.Closed)
    val scope = rememberCoroutineScope()
    ModalNavigationDrawer(
        modifier = Modifier.fillMaxSize(0.5F),
        scrimColor = Color.Cyan,
        drawerState = drawerState,
        drawerContent = {
            Button(
                modifier = Modifier.padding(top = 16.dp),
                onClick = { scope.launch { drawerState.close() } },
                content = { Text("关闭") }
            )
        },
        content = {
            Button(onClick = { scope.launch { drawerState.open() } }) {
                Text("打开")
            }
        }
    )
}